package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.breadtrip.R;

/**
 * Created by sunsg on 2017/4/6.
 */

public class BitmapShaderTestView extends View{
    private Paint mPaint;
    private Context mContext;
    public BitmapShaderTestView(Context context) {
        super(context);
        init(context);
    }

    public BitmapShaderTestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BitmapShaderTestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BitmapShaderTestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context){
        mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        drawRoundRectShader(canvas);
//        drawLinearGradient(canvas);
//        drawLinearGradientShoadow(canvas);
//        sweepGradient(canvas);
//        radialGradient(canvas);
          composeshader(canvas);
//        matrixShader(canvas);
    }

    private void drawRoundRectShader(Canvas canvas){
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.kale);
        float[] src = {0, 0};
        int DX = 300;
        float[] dst = {0 + DX, DX};
//         实例化一个矩阵对象
        Matrix matrix = new Matrix();
//        matrix.setPolyToPoly(src, 0, dst, 0, 1);
//        matrix.setRotate(90,bitmap.getWidth()/2,bitmap.getHeight()/2);
//        matrix.postSkew(0.5f,0);
//        matrix.setScale(1.5f,1.0f);
        matrix.postSkew(0.5f,0);
        canvas.drawBitmap(bitmap, matrix, mPaint);

        /*2个点*/
//        int bw = bitmap.getWidth();
//        int bh = bitmap.getHeight();
//        Matrix matrix = new Matrix();
//        float[] src = {0, 0, bw, 0};
//        float[] dst = {0, 0, bw*2, 0};
//        matrix.setPolyToPoly(src, 0, dst, 0, 2);
//        canvas.drawBitmap(bitmap, matrix, mPaint);
        /*2个点*/

        /*4个点*/
//        Matrix matrix = new Matrix();
//        int bw = bitmap.getWidth();
//        int bh = bitmap.getHeight();
//        float[] src = {0, 0, 0, bh, bw, bh, bw, 0};
//        int DX = bw/8;
//        float[] dst = {0 + DX, 0, 0, bh, bw, bh, bw - DX, 0};
//        matrix.setPolyToPoly(src, 0, dst, 0, 4);
//        canvas.drawBitmap(bitmap, matrix, mPaint);
// /*4个点*/

//        canvas.drawBitmap(bitmap, 0,0,null);
        // 设置shader
//        mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
//        // 用设置好的画笔绘制一个矩形
////        canvas.drawRect(0, 0, 600, 600, mPaint);
////        canvas.drawRoundRect(0,0,600,600,20,20,mPaint);
//        canvas.drawRect(0,0,800,800,mPaint);
    }

    private void drawLinearGradient(Canvas canvas){
        Shader shader = new LinearGradient(0, 0, 0, 400,  Color.YELLOW,Color.RED, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
//        canvas.drawRect(0, 0, 400, 400, mPaint);
        shader = new LinearGradient(0, 0, 0, 700,
                new int[] { Color.RED, Color.YELLOW,Color.GREEN },
                new float[] { 0.1f, 0.2F,0.3f}, Shader.TileMode.MIRROR);
//        , Color.GREEN, Color.CYAN, Color.BLUE
        // 设置shader
        mPaint.setShader(shader);
        canvas.drawRect(0, 0, 700, 700, mPaint);
    }

    private void drawLinearGradientShoadow(Canvas canvas){

        mPaint.reset();
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.kale);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight= bitmap.getHeight();
        mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.drawRect(0,0,bitmapWidth,bitmapHeight,mPaint);

        int layer = canvas.saveLayer(0, 0, bitmapWidth, bitmapHeight*2, null, Canvas.ALL_SAVE_FLAG);
        // 实例化一个矩阵对象
        Matrix matrix = new Matrix();
        matrix.setScale(1F, -1F);
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
//        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        mPaint.setShader(new BitmapShader(bitmap2, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.translate(0,bitmapHeight);

        mPaint.setShader(new LinearGradient(0,0,0,bitmapHeight, Color.TRANSPARENT, Color.BLACK,Shader.TileMode.CLAMP));
        canvas.drawRect(0,0,bitmapWidth,bitmapHeight/4.0f,mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        canvas.drawBitmap(bitmap2,0,0,mPaint);

//
        mPaint.setXfermode(null);
        canvas.restoreToCount(layer);
    }


    private void sweepGradient(Canvas canvas){
        mPaint.reset();
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.kale);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight= bitmap.getHeight();
        mPaint.setShader(new SweepGradient(400,400,new int[]{Color.GREEN,Color.WHITE},new float[]{0.1f,0.9f}));
        canvas.drawCircle(400,400,400,mPaint);
    }

    private void radialGradient(Canvas canvas){
        Shader shader = new RadialGradient(400, 400, 400, new int[]{Color.RED, Color.GREEN,Color.BLUE},new float[]{0.2f,0.5f,0.8f},Shader.TileMode.CLAMP);
        // 设置shader
        mPaint.setShader(shader);
        canvas.drawCircle(400, 400,400, mPaint);
    }
    private int composesBitmapHeight = -1;
    private void composeshader(Canvas canvas){

//        Shader shader01 = new RadialGradient(200, 200, 200, Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
//        Shader shader02 = new SweepGradient(200, 200, new int[] { Color.BLUE, Color.YELLOW, Color.GREEN }, null);
//
//        // 设置shader
//        mPaint.setShader(new ComposeShader(shader01, shader02, PorterDuff.Mode.DARKEN));
//
//        canvas.drawRect(0, 0, 400, 400, mPaint);
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.hert);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight= bitmap.getHeight();
        if(composesBitmapHeight == -1){
            composesBitmapHeight = 0;
        }
        //将绘制代码放入到canvas.saveLayer()和canvas.restore()之间
        canvas.saveLayer(0, 0, bitmapWidth, bitmapHeight, null, Canvas.ALL_SAVE_FLAG);
        //创建BitmapShader，用以绘制?形
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        //将BitmapShader作为画笔paint绘图所使用的shader
        mPaint.setShader(bitmapShader);
        //用BitmapShader绘制矩形
        canvas.drawRect(0, 0, bitmapWidth, bitmapHeight, mPaint);
        //将画笔的Xfermode设置为PorterDuff.Mode.MULTIPLY模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
        //创建LinearGradient，用以产生从左上角到右下角的颜色渐变效果
        LinearGradient linearGradient = new LinearGradient(0, 0, bitmapWidth, bitmapHeight, Color.GREEN, Color.GREEN, Shader.TileMode.CLAMP);
        //将创建LinearGradient作为画笔paint绘图所使用的shader
        mPaint.setShader(linearGradient);
        //用LinearGradient绘制矩形
        composesBitmapHeight+=bitmapHeight/20;
        if(composesBitmapHeight >= bitmapHeight){
            composesBitmapHeight = 0;
        }
        canvas.drawRect(0, bitmapHeight-composesBitmapHeight, bitmapWidth, bitmapHeight*2-composesBitmapHeight , mPaint);
        //最后将画笔去除掉Xfermode
        mPaint.setXfermode(null);
        canvas.restore();

        postInvalidateDelayed(100);
    }

    private void matrixShader(Canvas canvas){
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.kale);

        Matrix matrix = new Matrix();
        // 设置矩阵变换
        matrix.setTranslate(100, 100);
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        // 设置Shader的变换矩阵
        bitmapShader.setLocalMatrix(matrix);
        // 设置shader
        mPaint.setShader(bitmapShader);
        // 用设置好的画笔绘制一个矩形
//        canvas.drawRect(0, 0, 600, 600, mPaint);
//        canvas.drawRoundRect(0,0,600,600,20,20,mPaint);
        canvas.drawRect(0,0,800,800,mPaint);
    }


}
